#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>

struct ListNode
{
	int val;
	struct ListNode* next;
};
struct ListNode* MiddleNode(struct ListNode* head)
{
	struct ListNode* slow, * fast;
	slow = fast = head;
	while (fast && fast->next)
	{
		fast = fast->next->next;
		slow = slow->next;
	}
	return slow;
}

struct ListNode* CreateList(int* a, int n)
{
	struct  ListNode* phead = NULL, * ptail = NULL;
	int x = 0;
	for (int i = 0; i < n; ++i)
	{
		struct ListNode* newnode = (struct ListNode*)malloc(sizeof(struct ListNode));
		if (newnode == NULL)
		{
			perror("malloc fail");
			exit(-1);
		}
		else
		{
			newnode->val = a[i];
			newnode->next = NULL;
		}
		if (phead == NULL)
		{
			ptail = phead = newnode;
		}
		else
		{
			ptail->next = newnode;
			ptail = newnode;
		}
	}
	return phead;
}
int main()
{
	int a[] = { 1,2,3,4,5 };
	struct ListNode* plist = CreateList(a, sizeof(a) / sizeof(int));
	MiddleNode(plist);
	return 0;
}